﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] readFromFile = File.ReadAllLines(".//..//..//in.txt");
            int N = readFromFile.Length;
            Program p = new Program();
            int[,] arr2D = p.ParseIntArr2D(readFromFile, N);  //получаю 2-мерный массив интов
            int[,] arr2DD = p.DiagonalChange(arr2D, N);                 // меняю элементы массива на на суммы строк 
            string[] res = p.Arr2DToString(arr2DD);
            File.WriteAllLines(".//..//..//out.txt", res);
            Console.WriteLine("Great job!");
            Console.WriteLine();

        }


        public int[,] ParseIntArr2D(string[] readFromFile, int N) // form 2D array
        {
            int[,] arr2D = new int[N, N];
            string[] tmp = new string[N];

            for (int i = 0; i < readFromFile.Length; i++)
            {
                tmp = readFromFile[i].Split(',');
                for (int j = 0; j < readFromFile.Length; j++)
                {
                    arr2D[i, j] = Convert.ToInt32(tmp[j]);
                }
            }
            return arr2D;
        }

        public int[,] DiagonalChange(int[,] arr2D, int N) // summ of elements and place to diagonal
        {

            for (int i = 0; i < arr2D.GetLength(0); i++)
            {
                int sumStr = 0;
                for (int j = 0; j < arr2D.GetLength(1); j++)
                {
                    sumStr=sumStr + arr2D[i, j];
                }
                arr2D[i, i] = sumStr;
            }
            return arr2D;
        }



        /*  public int[,] DiagonalChange(int[,] arr2D, int[] sum)
          {
              for (int i = 0; i < arr2D.GetLength(0); i++)
              {
                  for (int j = 0; j < arr2D.GetLength(1); j++)
                  {
                      if (i == j)
                      {
                          arr2D[i, j] = sum[i];
                      }
                  }
              }
              return arr2D;
          }*/


        public string[] Arr2DToString(int[,] arr2D)
        {
            string[] res = new string[arr2D.GetLength(0)];
            int[] tmp = new int[arr2D.GetLength(1)];

            for (int i = 0; i < arr2D.GetLength(0); i++)
            {
                for (int j = 0; j < arr2D.GetLength(1); j++)
                {
                    tmp[j] = arr2D[i, j];
                }
                string s = string.Join(",", tmp);
                res[i] = s;
            }
            return res;
        }
    }
}
